package control.panel
{
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	
	import global.Emote;
	import global.GameState;
	
	import utils.EnterFrameTimer;
	
	import view.panel.emote.EmoteItem;
	import view.panel.emote.EmotePanel;

	public class EmotePanelControl extends PanelControl
	{
		private var items:Array;
		private var closeTimer:EnterFrameTimer;
		
		public function EmotePanelControl()
		{
			initPanel();
			initItems();
			initEmote();
			initTimer();
		}
		
		private function initTimer():void
		{
			closeTimer = new EnterFrameTimer(1000);
			closeTimer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			closeTimer.reset();
			_view.emotePanel.close();
		}
		
		private function initEmote():void
		{
			var imgKey:String;
			var item:EmoteItem
			for (var i:String in Emote.EmoteImgKey)
			{
				imgKey = Emote.EmoteImgKey[i];
				item = items[i];
				item.setImg(imgKey);
				item.info = Emote.EmoteInfo[i];
			}
		}
		
		private function initPanel():void
		{
			_view.emotePanel = new EmotePanel(this);
			_view.emotePanel.addEventListener(MouseEvent.ROLL_OUT,onRollOut);
			_view.emotePanel.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
		}
		
		private function onRollOver(event:MouseEvent):void
		{
			closeTimer.reset();
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			closeTimer.reset();
			closeTimer.start();
		}
		
		private function initItems():void
		{
			items = [];
			var item:EmoteItem
			for (var i:int = 0; i<4; i++)
			{
				for (var j:int = 0; j<5; j++)
				{
					item = new EmoteItem(null);
					item.x = j * 44 + 8;
					item.y = i * 44 + 8;
					item.addEventListener(MouseEvent.CLICK,onItemClick);
					_view.emotePanel.addChild(item);
					items.push(item);
				}
			}
		}
		
		private function onItemClick(event:MouseEvent):void
		{
			var faceKey:String = event.target.key;
			_model.netRemote.sendFace(faceKey);
			_view.emotePanel.close();
			
			if ( !GameState.isSited)
			{
				_view.playPanel.updateServerMsg("[系统]:对不起，只有坐下才能发送表情。");
			}
		}
		
		public function open(...args):void
		{
			_view.emotePanel.open();
			
			for each (var item:EmoteItem in items)
			{
				item.enabled = GameState.isSited;
			}
			
		}
	}
}